package com.dex4d.ui.main.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioGroup;

import com.dex4d.R;
import com.dex4d.core.AppToolBarBaseActivity;
import com.dex4d.model.D4DTxLogModel;
import com.dex4d.ui.main.model.TokenDetail;
import com.dex4d.ui.main.socket.RPC;
import com.dex4d.ui.main.socket.RPCParamList;
import com.dex4d.ui.main.socket.Receipt;
import com.dex4d.utils.BaseUtils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by chengyuchun on 2018/11/27.
 */
public class D4DTransactionActivity extends AppToolBarBaseActivity {
    @BindView(R.id.rv_d4d_tx)
    RecyclerView rv_d4d_tx;

    @BindView(R.id.group_type)
    RadioGroup group_type;

    @BindView(R.id.group_status)
    RadioGroup group_status;

    private CommonAdapter mCommonAdapter;
    private List<TokenDetail> mTxShowList = new ArrayList<TokenDetail>();
    private Receipt mReceipt = new Receipt();
    private String mType = "All";
    private String mStatus = "All";
    @Override
    protected void initToolbar() {
        showBackView();
        showTitle(getString(R.string.d4d_tx_transaction));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_d4d_transaction;
    }

    @Override
    protected void initVariables() {
        EventBus.getDefault().register(this);
    }

    @Override
    protected void loadData() {
        mReceipt.send();

        mCommonAdapter = new CommonAdapter<TokenDetail>(this, R.layout.item_d4d_transaction, mTxShowList) {
            @Override
            protected void convert(ViewHolder holder, final TokenDetail tokenDetail, int position) {
                holder.setText(R.id.tv_trade_pair, tokenDetail.Tokens.toUpperCase());
                holder.setText(R.id.tv_type, "("+getTypeShowName(tokenDetail.type)+")");
                holder.setText(R.id.tv_status, getStatusShowName(tokenDetail.status));
                holder.setText(R.id.tv_time, BaseUtils.formatTime(tokenDetail.time));
                holder.setText(R.id.tv_trans_id, BaseUtils.formatTxHash(tokenDetail.id));
            }
        };
        rv_d4d_tx.setLayoutManager(new LinearLayoutManager(this));
        rv_d4d_tx.setAdapter(mCommonAdapter);
    }

    private String getTypeShowName(String type){
        if(D4DTxLogModel.TradeType.BUY.getType().equalsIgnoreCase(type)){
            return getString(R.string.d4d_tx_type_buy);
        }else if(D4DTxLogModel.TradeType.REINVEST.getType().equalsIgnoreCase(type)){
            return getString(R.string.d4d_tx_type_Reinvest);
        }else if(D4DTxLogModel.TradeType.SELL.getType().equalsIgnoreCase(type)){
            return getString(R.string.d4d_tx_type_sell);
        }else if(D4DTxLogModel.TradeType.SWAP.getType().equalsIgnoreCase(type)){
            return getString(R.string.d4d_tx_type_swap);
        }else if(D4DTxLogModel.TradeType.WITHDRAW.getType().equalsIgnoreCase(type)){
            return getString(R.string.d4d_tx_type_withdraw);
        }else {
            return type;
        }
    }

    private String getStatusShowName(String status){
        if(D4DTxLogModel.TxStatus.PENDING.getStatus().equalsIgnoreCase(status)){
            return getString(R.string.d4d_tx_status_pending);
        }else if(D4DTxLogModel.TxStatus.SUCCESS.getStatus().equalsIgnoreCase(status)){
            return getString(R.string.d4d_tx_status_success);
        }else if(D4DTxLogModel.TxStatus.FAILED.getStatus().equalsIgnoreCase(status)){
            return getString(R.string.d4d_tx_status_failed);
        }else {
            return status;
        }
    }

    private void notifyToShowList(List<TokenDetail> pushList){
        mTxShowList.clear();
        mTxShowList.addAll(filterShowList(pushList));
        mCommonAdapter.notifyDataSetChanged();
    }

    private List<TokenDetail> filterShowList(List<TokenDetail> pushList){
        return filterStatusList(filterTypeList(pushList));
    }

    private List<TokenDetail> filterTypeList(List<TokenDetail> list){
        List<TokenDetail> filterTypeList = new ArrayList<TokenDetail>();
        if(mType.equalsIgnoreCase("All")){
            return list;
        }else {
            for(TokenDetail tokenDetail:list){
                if(tokenDetail.type.equalsIgnoreCase(mType)){
                    filterTypeList.add(tokenDetail);
                }
            }
        }
        return filterTypeList;
    }

    private List<TokenDetail> filterStatusList(List<TokenDetail> list){
        List<TokenDetail> filterStatusList = new ArrayList<TokenDetail>();
        if(mStatus.equalsIgnoreCase("All")){
            return list;
        }else {
            for(TokenDetail tokenDetail:list){
                if(tokenDetail.status.equalsIgnoreCase(mStatus)){
                    filterStatusList.add(tokenDetail);
                }
            }
        }
        return filterStatusList;
    }

    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    public void updateTransData(RPC rpc) {
        if (rpc != null && rpc.params != null) {
            if (TextUtils.equals(rpc.tag, "personTradingList")) {
                RPCParamList rpcParam = rpc.params;
                if (rpcParam.result != null && !rpcParam.result.isEmpty()) {
                    notifyToShowList(rpcParam.result);
                }
            }
        }
    }

    @OnClick({R.id.radio_type_all,R.id.radio_type_buy,R.id.radio_type_reinvest,
            R.id.radio_type_sell,R.id.radio_type_swap,R.id.radio_type_withdraw,
            R.id.radio_status_all,R.id.radio_status_pending,R.id.radio_status_success,R.id.radio_status_failed})
    public void onViewClick(View view){
        switch (view.getId()){
            case R.id.radio_type_all:
                mType = "All";
                break;
            case R.id.radio_type_buy:
                mType = D4DTxLogModel.TradeType.BUY.getType();
                break;
            case R.id.radio_type_reinvest:
                mType = D4DTxLogModel.TradeType.REINVEST.getType();
                break;
            case R.id.radio_type_sell:
                mType = D4DTxLogModel.TradeType.SELL.getType();
                break;
            case R.id.radio_type_swap:
                mType = D4DTxLogModel.TradeType.SWAP.getType();
                break;
            case R.id.radio_type_withdraw:
                mType = D4DTxLogModel.TradeType.WITHDRAW.getType();
                break;
            case R.id.radio_status_all:
                mStatus = "All";
                break;
            case R.id.radio_status_pending:
                mStatus = D4DTxLogModel.TxStatus.PENDING.getStatus();
                break;
            case R.id.radio_status_success:
                mStatus = D4DTxLogModel.TxStatus.SUCCESS.getStatus();
                break;
            case R.id.radio_status_failed:
                mStatus = D4DTxLogModel.TxStatus.FAILED.getStatus();
                break;
        }
        mReceipt.send();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
